package com.ruoyi.common.utils;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.sql.SqlUtil;

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils {
	/**
	 * 设置请求分页数据
	 */
	public static Pageable startPage() {
		PageDomain pageDomain = TableSupport.buildPageRequest();
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
		boolean isAsc = pageDomain.getIsAsc();
		if(StringUtils.isNotEmpty(orderBy)){
			return PageRequest.of(pageNum-1, pageSize,Sort.by(isAsc ? Direction.ASC : Direction.DESC,orderBy));
		}else{
			return PageRequest.of(pageNum-1, pageSize);
		}

	}

}
